package cn.b.sky.mes.cm.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.mes.cm.model.WorkCenterType;
import cn.b.sky.mes.cm.service.WorkCenterTypeService;
import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.enums.DwzCallbackType;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.common.model.TreeInterface;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;

@Controller
@RequestMapping(value = "/workCenterType/*")
public class WorkCenterTypeController extends SecurityController {
  @Resource
  WorkCenterTypeService workCenterTypeService;

  @RequestMapping("workCenterTypeList")
  public String workCenterTypeList(Model model, String navTabId, String selectNode) {
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("selectNode", selectNode);
    List<TreeInterface> types = workCenterTypeService.selectAllWorkCenterType(SecurityContextUtil.getSiteCode());
    model.addAttribute("treeList", types);
    return "cm/workCenterType/workCenterType_list";
  }

  @RequestMapping("workCenterTypeLookup")
  public String workCenterTypeLookup(Model model, String navTabId) {
    model.addAttribute(NAVTABID, navTabId);
    List<TreeInterface> types = workCenterTypeService.selectAllWorkCenterType(SecurityContextUtil.getSiteCode());
    model.addAttribute("treeList", types);
    return "cm/workCenterType/workCenterType_lookup";
  }

  @RequestMapping("addWcType")
  public String addWcType(Model model, String navTabId, String id) {
    model.addAttribute(NAVTABID, navTabId);
    if (!StringUtils.isBlank(id)) {
      WorkCenterType type = workCenterTypeService.selectWorkCenterType(SecurityContextUtil.getSiteCode(), id);
      WorkCenterType t = new WorkCenterType();
      t.setParentTypeCode(id);
      t.setParentTypeText(type.getTypeText());
      model.addAttribute("type", t);
    }
    return "cm/workCenterType/workCenterType_add";
  }

  @RequestMapping("validateWorkCenterTypeCode")
  @ResponseBody
  public boolean validateWorkCenterTypeCode(String workCenterTypeCode, Model model) {
    if (!StringUtils.isBlank(workCenterTypeCode)) {
      WorkCenterType type = workCenterTypeService.selectWorkCenterType(SecurityContextUtil.getSiteCode(),
          workCenterTypeCode);
      return null == type;
    }
    return true;

  }

  @RequestMapping("editWcType")
  public String editWct(Model model, String navTabId, String id) {
    WorkCenterType type = workCenterTypeService.selectWorkCenterType(SecurityContextUtil.getSiteCode(), id);
    if (type != null && !StringUtils.isEmpty(type.getParentTypeCode())) {
      WorkCenterType ptype = workCenterTypeService.selectWorkCenterType(SecurityContextUtil.getSiteCode(),
          type.getParentTypeCode());
      type.setParentTypeText(ptype.getTypeText());
    }
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("type", type);
    return "cm/workCenterType/workCenterType_add";

  }

  @RequestMapping("viewWcType")
  public String viewWcType(Model model, String navTabId, String id) {
    WorkCenterType type = workCenterTypeService.selectWorkCenterType(SecurityContextUtil.getSiteCode(), id);
    if (type != null && !StringUtils.isEmpty(type.getParentTypeCode())) {
      WorkCenterType ptype = workCenterTypeService.selectWorkCenterType(SecurityContextUtil.getSiteCode(),
          type.getParentTypeCode());
      type.setParentTypeText(ptype.getTypeText());
    }
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("type", type);
    return "cm/workCenterType/workCenterType_view";

  }

  @RequestMapping("saveWcType")
  @ResponseBody
  public DWZResult saveWcType(Model model, String navTabId, String id, WorkCenterType type, HttpServletRequest request) {
    if (type == null) {
      return ajaxError(getImeViewMessageI18n(null, SystemConstant.PARAMETERNULL));
    }
    type.setSiteCode(SecurityContextUtil.getSiteCode());
    setCreateInfo(type);
    if (StringUtils.isEmpty(id)) {
      workCenterTypeService.insertWorkCenterType(type);
    } else {
      workCenterTypeService.updateWorkCenterType(type);
    }
    String forwardUrl = getForwardUrl(request) + "/workCenterType/workCenterTypeList?navTabId=" + navTabId
        + "&selectNode=" + type.getWorkCenterTypeCode();
    return navTabAjaxDone(getImeViewMessageI18n(null, "successfulOperation.M"), navTabId, null,
        DwzCallbackType.FORWARD, forwardUrl, null, null);

  }

  @RequestMapping("deleteWcType")
  @ResponseBody
  public DWZResult deleteWcType(Model model, String navTabId, String id, HttpServletRequest request) {
    if (StringUtils.isEmpty(id)) {
      return ajaxError(getImeViewMessageI18n(null, SystemConstant.PARAMETERNULL));
    }
    workCenterTypeService.deleteWorkCenterType(SecurityContextUtil.getSiteCode(), id);
    return navTabAjaxDone(navTabId, null, null, null, getImeViewMessageI18n(null, "successfulOperation.M"), null);
  }
}
